from os import path
import numpy as np
import os
import shutil
from sklearn.decomposition import PCA
from joblib import Parallel, delayed

import utils

def create_train_matrix(i):
	dir_name="classified_train_data/"+str(i)
	file_name="models/original_train_matrix_"+str(i)
	utils.save_images_to_matrix(i,dir_name,file_name)
def pca_train_matrix(i,dim):
	file_name="models/original_train_matrix_"+str(i)+"_new"
	src=utils.load_matrix(file_name)
	pca=PCA(n_components=dim)
	pca_out=pca.fit_transform(src)
	out_name="models/pca_train_matrix_"+str(i)+"_"+str(dim)
	utils.save_matrix(pca_out,out_name)
		
def create_test_matrix(i):
	dir_name="classified_test_data/"+str(i)
	file_name="models/original_test_matrix_"+str(i)
	utils.save_images_to_matrix(i,dir_name,file_name)

def pca_test_matrix(i,dim):
	model_file_name="models/original_train_matrix_"+str(i)+"_new"
	model_src=utils.load_matrix(model_file_name)
	pca=PCA(n_components=dim)
	model=pca.fit_transform(model_src)
	test_data_file_name="models/original_test_matrix_"+str(i)+"_new"
	test_src=utils.load_matrix(test_data_file_name)
	pca_out=pca.transform(test_src)
	out_name="models/pca_test_matrix_"+str(i)+"_"+str(dim)
	utils.save_matrix(pca_out,out_name)

if __name__ == "__main__":
	#Parallel(n_jobs=2)(delayed(create_train_matrix)(p) for p in range(10))
	Parallel(n_jobs=2)(delayed(pca_train_matrix)(p,30) for p in range(10))
	#Parallel(n_jobs=2)(delayed(create_test_matrix)(p) for p in range(10))
	Parallel(n_jobs=2)(delayed(pca_test_matrix)(p,30) for p in range(10))
